package com.eedi.org.hospital.controller;

import com.eedi.framework.common.pojo.CommonResult;
import com.eedi.framework.common.pojo.PageResult;
import com.eedi.framework.common.util.object.BeanUtils;
import com.eedi.framework.common.validation.AddGroup;
import com.eedi.framework.common.validation.UpdateGroup;
import com.eedi.org.hospital.dal.dataobject.OrgHospitalDO;
import com.eedi.org.hospital.service.OrgHospitalService;
import com.eedi.org.hospital.controller.vo.hosp.OrgHospitalPageReqVO;
import com.eedi.org.hospital.controller.vo.hosp.OrgHospitalRespVO;
import com.eedi.org.hospital.controller.vo.hosp.OrgHospitalSaveReqVO;
import com.eedi.org.hospital.controller.vo.hosp.OrgHospitalUpdateStatusReqVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.annotation.security.PermitAll;
import javax.validation.Valid;
import java.util.List;

import static com.eedi.framework.common.pojo.CommonResult.success;


@Tag(name = "管理后台 - 医院")
@RestController
@RequestMapping("/hospital")
@Validated
public class OrgHospitalController {

    @Resource
    private OrgHospitalService orgHospitalService;

    @PostMapping("/create")
    @Operation(summary = "创建医院")
 //   @PreAuthorize("@ss.hasPermission('org:hospital:create')")
    public CommonResult<String> createHospital(@Validated({AddGroup.class}) @RequestBody OrgHospitalSaveReqVO createReqVO) {
        return success(orgHospitalService.createHospital(createReqVO));
    }

    @PostMapping("/update")
    @Operation(summary = "更新医院")
//    @PreAuthorize("@ss.hasPermission('org:hospital:update')")
    public CommonResult<Boolean> updateHospital(@Validated({UpdateGroup.class}) @RequestBody OrgHospitalSaveReqVO updateReqVO) {
        orgHospitalService.updateHospital(updateReqVO);
        return success(true);
    }

    @GetMapping("/delete")
    @Operation(summary = "删除医院")
    @Parameter(name = "orgHospitalId", description = "编号", required = true)
 //   @PreAuthorize("@ss.hasPermission('org:hospital:delete')")
    public CommonResult<Boolean> deleteHospital(String orgHospitalId) {
        orgHospitalService.deleteHospital(orgHospitalId);
        return success(true);
    }


    @PostMapping("/update-status")
    @Operation(summary = "修改医院状态")
    //   @PreAuthorize("@ss.hasPermission('org:hospital:updateStatus')")
    @PermitAll
    public CommonResult<Boolean> updateHospitalStatus(@Valid @RequestBody OrgHospitalUpdateStatusReqVO reqVO) {
        orgHospitalService.updateHospitalStatus(reqVO.getOrgHospitalId(), reqVO.getOrgStatus());
        return success(true);
    }

    @GetMapping("/list")
  //  @PreAuthorize("@ss.hasPermission('org:hospital:list')")
    public CommonResult<List<OrgHospitalRespVO>> listHospital() {
        List<OrgHospitalDO> orgHospitalDOS = orgHospitalService.listHospital();
        return success(BeanUtils.toBean(orgHospitalDOS, OrgHospitalRespVO.class));
    }

    @PostMapping("/page")
  //  @PreAuthorize("@ss.hasPermission('org:hospital:query')")
    public CommonResult<PageResult<OrgHospitalRespVO>> getHospitalPage(@RequestBody @Valid OrgHospitalPageReqVO pageReqVO) {
        PageResult<OrgHospitalDO> pageResult = orgHospitalService.getHospitalPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, OrgHospitalRespVO.class));
    }


}
